
var app = getApp();
Page({
	data: {
		img_url: 'http://17fx.com/imgs/',
		number: 1,
		len: 0,
		payType: 'hb',
		checkhb: false,
		checkwx: true,
		checkqb: false,
		pay: false,
		focus: false,
    goodsid:0,
    goods_name:null,
    shop_price:0,
    shop_prices:0
	},
  onLoad: function (options) {
    //判断是否登录，全局变量是否为空
    if (getApp().globalData.openid == null ){
      //登录调用
      getApp().getUserInfo(function (userInfo, openid1) {})
        
    }
    console.log('页面传过来good的参数' + options.id)
    this.setData({
      goodsid: options.id
    })
    // 生命周期函数--监听页面加载
    var that = this
    wx.request({
      url: 'https://17fx.com/weapp/api/goodsinfo.html',
      data: {
        id: options.id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)

        that.setData({
          goods_name: res.data[0].goods_name,
          shop_price: res.data[0].shop_price,
          shop_prices: res.data[0].shop_price
        });
      }
    });
	},
	onShow: function () {
		wx.setNavigationBarTitle({
			title: ''
		})
	},
	tapCheck: function (e) {
		var id = e.currentTarget.dataset.id;
		if (id == '1') {
			this.setData({
				checkhb: true,
				checkwx: false,
				checkqb: false
			})
		} else if (id == '2') {
			this.setData({
				checkhb: false,
				checkwx: true,
				checkqb: false
			})
		} else {
			this.setData({
				checkhb: false,
				checkwx: false,
				checkqb: true
			})
		}
	},
  //计数价格
	numTap: function (e) {
		// var id = e.currentTarget.id;
		// var num = this.data.number;
    // var price = this.data.shop_price;
		// if (id == '1') {
		// 	if (num == 0) {
		// 		this.setData({
		// 			number: num
		// 		})
		// 	} else {
		// 		this.setData({
		// 			number: num - 1
		// 		})
		// 	}
		// } else {
		// 	this.setData({
		// 		number: num + 1
		// 	})
		// }
    // var num1 = this.data.number;
    // var prices = price * num1;
    // this.setData({
    //   shop_prices: prices
    // })

    wx.showModal({
      title: '提示',
      content: '一次只能购买一份',
      showCancel:false,
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
	},

  //微信支付 发起微信支付
  formSubmit: function (e) {
    console.log('支付字段' + e.detail.value['number']);
    console.log('支付字段' + e.detail.value['shop_prices']);
    console.log('支付字段' + e.detail.value['goodsid']);
    var number = e.detail.value['number'];
    var shop_prices = e.detail.value['shop_prices'];
    var goods_id = e.detail.value['goodsid'];
		var pay = this.data.checkwx;
    var that = this;
		if (pay) {
      //调用微信支付getpayment
      console.log('参数' + getApp().globalData.openid)
      GetList3(that,getApp().globalData.openid,number,shop_prices,goods_id)

		} else {
			this.setData({
				focus: true,
				pay: true
			})
		}
	},
	bindHideKeyboard: function (e) {
		var lens = e.detail.value.length;
		this.setData({
			len: lens
		})
		if (e.detail.cursor == 6) {
			wx.hideKeyboard();
			this.setData({
				focus: false,
				pay: false
			})
		}
	}
});

var url3 = "https://17fx.com/weapp/api/getPayment.html";
//  调取微信支付
var GetList3 = function (that, openid, number, shop_prices, goods_id) {
  wx.request({
    url: url3,
    data: {
      openid: openid,
      number:number,
      shop_prices:shop_prices,
      goods_id:goods_id,
      buy_leader: getApp().globalData.suid
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      // success
      console.log(res.data);
      console.log("调用支付123");
      that.setData({
        
        //prepay_id:res.data['prepay_id'],
        //token:res.data['token'],
        //uopenid:res.data['uopenid'],
        //packages:res.data['package'],
        //time:res.data['time']
      })

      //弹出提示，库存不足，每人每个商品超出购买数量
      if(res.data == 0){
        wx.showModal({
          title: '提示',
          content: '已售完，请关注17分享商城公众号获取执行抢购优惠',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
      if(res.data == 1){
        wx.showModal({
          title: '提示',
          content: '一次只能购买一份',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
      if(res.data == 2){
        wx.showModal({
          title: '提示',
          content: '只能购买一份',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
      if (res.data == 3) {
        wx.showModal({
          title: '提示',
          content: '暂未开始',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
      //支付
      wx.requestPayment({
          'timeStamp': res.data['timeStamp'],
          'nonceStr': res.data['nonceStr'],
          'package': res.data['package'],
          'signType': 'MD5',
          'paySign': res.data['paySign'],
          'success':function(res){
              console.log("zhifu成功");
              console.log(res);
              console.log(res.data);
              //跳转到订单页面（导航页）
              wx.switchTab({
                url: '/pages/order/order'
              })
 



      //发送模板消息
      // var fId = that.data.prepay_id;
      // console.log(that.data.prepay_id);
      // var l = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + that.data.token;
      // var d = {  
      // "touser": that.data.uopenid,
      // "template_id": 'XL4mtVOlGtG7GN7gZAxZbvHDDNk6HgR_rcxkThuHmcU',//这个是1、申请的模板消息id，  
      // "page": '/pages/index/index',  
      // "form_id": fId,  
      // "data": {//测试完发现竟然value或者data都能成功收到模板消息发送成功通知，下面的keyword*是你1、设置的模板消息的关键词变量  

      //     "keyword1": {  
      //     "value": "探陌爱心",  
      //     "color": "#4a4a4a"  
      //     },  
      //     "keyword2": {  
      //     "value": "爱心点赞",  
      //     "color": "#9b9b9b"  
      //     },  
      //     "keyword3": {  
      //     "value": that.data.time,  
      //     "color": "#9b9b9b"  
      //     }
      // },  
      // color: '#ccc',  

      // }  
      // wx.request({
      //     url: l,
      //     data: d,
      //     method: 'POST',
      //     success: function(res){
      //         console.log("push msg");
      //         console.log(res);
      //     },
      //     fail: function(err) {
      //         // fail
      //         console.log("push err")
      //         console.log(err);
      //     }
      //     });




          },
          'fail':function(res){
              console.log("zhifu失败");
              console.log(res);
          }
      })
    },
    fail: function () {
      // fail
      console.log("调用微信支付接口出错");
    },
    complete: function () {
      // complete
    }
  })
}